package com.coolweather.android.gson;

import android.text.TextUtils;

import com.coolweather.android.db.City;
import com.coolweather.android.db.County;
import com.coolweather.android.db.Province;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2017/12/7 0007.
 */

public class Utility {
    public static boolean handleProviceData(String response){
        System.out.print(response);
        if (!TextUtils.isEmpty(response)){
            JSONArray allCitys = null;
            try {
                allCitys = new JSONArray(response);
                for (int i=0;i<allCitys.length();i++){
                    JSONObject provice = allCitys.getJSONObject(i);
                    Province provice1 = new Province();
                    provice1.setProvinceCode(provice.getInt("id"));
                    provice1.setProvinceName(provice.getString("name"));
                    provice1.save();
                }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    public static boolean handleCityData(String response,int proviceId){
        System.out.print(response);
        if (!TextUtils.isEmpty(response)){
            JSONArray allCitys = null;
            try {
                allCitys = new JSONArray(response);
                 for (int i=0;i<allCitys.length();i++){
                    JSONObject city = allCitys.getJSONObject(i);
                    City city1 = new City();
                    city1.setCityName(city.getString("name"));
                    city1.setCityCode(city.getInt("id"));
                    city1.setProvinceId(proviceId);
                    city1.save();
                 }
                return true;
            } catch (JSONException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;

    }

    public static boolean handleCountyData(String response,int cityId){
        System.out.print(response);
        if (!TextUtils.isEmpty(response)){
            JSONArray allCitys = null;
            try {
                allCitys = new JSONArray(response);
                for (int i=0;i<allCitys.length();i++) {
                    JSONObject county = allCitys.getJSONObject(i);
                    County county1 = new County();
                    county1.setCountyName(county.getString("name"));
                    county1.setWeatherId(county.getString("weather_id"));
                    county1.setCityId(cityId);
                    county1.save();
                }
                return true;
            } catch (JSONException e) {
                    e.printStackTrace();
                    return false;
            }
        }
        return false;
    }

    public static Weather handleWeatherResponse(String response){
        try {
            System.out.print(response);
            JSONObject jsonObject = new JSONObject(response);
            JSONArray jsonArray =  jsonObject.getJSONArray("HeWeather");
            String weatherString = jsonArray.getJSONObject(0).toString();
            return new Gson().fromJson(weatherString,Weather.class);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
